package com.run.guifeng.controller.smsController;


import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Controller
@RequestMapping("/sms")
public class SmsController {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @GetMapping("/{phone}")
    public void Sendms(@PathVariable String phone, HttpSession httpSession){
        //六位的验证码
        int bound = 1000000;
        Random random = new Random();
        int checkCode = random.nextInt(bound);
        if(checkCode<100000){
            checkCode+=100000;
        }
        Map<String,String> map = new HashMap<>();
        map.put("phone",phone);
        map.put("code",checkCode+"");
//        CheckCode code = new CheckCode(phone,checkCode);
//        checkCodeService.save(code);
        httpSession.setAttribute("code",checkCode+"");
        System.out.println(checkCode);
        //发送验证码到rabbitmq
        rabbitTemplate.convertAndSend("userKey",map);
    }

}
